package com.gf.support.gateway.handler;

import com.gf.framework.common.exception.BizException;
import com.gf.framework.common.result.R;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.web.server.ResponseStatusException;

public class AbstractExceptionHandler {

    private static final String DEFAULT_ERROR_CODE = "9999";
    private static final String DEFAULT_ERROR_MSG = "服务器正忙，请稍后再试~";


    protected R<Void> buildErrorWrapper(Throwable ex) {
        String errorMessage ;
        String errorCode;
        if (ex instanceof NotFoundException) {
            // 接口路径404
            errorMessage = "服务迷路了~";
            errorCode = DEFAULT_ERROR_CODE;
        } else if(ex instanceof BizException){
            BizException mallBusinessException = (BizException) ex;
            errorMessage = mallBusinessException.getLocalizedMessage();
            errorCode = mallBusinessException.getCode();
        } else if (ex instanceof ResponseStatusException) {
            ResponseStatusException responseStatusException = (ResponseStatusException) ex;
            errorMessage = responseStatusException.getMessage();
            errorCode = DEFAULT_ERROR_CODE;
        } else {
            errorMessage = DEFAULT_ERROR_MSG;
            errorCode = DEFAULT_ERROR_CODE;
        }
        return R.fail(errorCode, errorMessage);
    }
}
